home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / SCHMOO.ZIP / OLEDOC.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  11KB  |  417 lines

  1. /*
  2.  * OLEDOC.C
  3.  *
  4.  * Contains all callback functions in the OLESERVERDOCVTBL struture:
  5.  *      DocClose
  6.  *      DocGetObject
  7.  *      DocExecute
  8.  *      DocRelease
  9.  *      DocSave
  10.  *      DocSetColorScheme
  11.  *      DocSetDocDimensions
  12.  *      DocSetHostNames
  13.  *
  14.  * Also contains two helper functions, PDocumentAllocate and DocumentClean.
  15.  * PDocumentAllocate acts like a C++ constructor.
  16.  *
  17.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  18.  *
  19.  */
  20.  
  21. #ifdef MAKEOLESERVER
  22.  
  23. #include <windows.h>
  24. #include <ole.h>
  25. #include "schmoo.h"
  26. #include "oleglobl.h"
  27.  
  28.  
  29. /*
  30.  * PDocumentAllocate
  31.  *
  32.  * Purpose:
  33.  *  Allocates a SCHMOODOC structure and sets the defaults in its fields.
  34.  *  Used from application initialization and various server methods that
  35.  *  create a document.
  36.  *
  37.  * Parameters:
  38.  *  pVtblDoc        LPOLESERVERDOCVTBL used to initialize the pvtbl field.
  39.  *
  40.  * Return Value:
  41.  *  LPSCHMOODOC     Pointer to the allocated structure in local memory.
  42.  *                  The hMem field will contain a handle to the structure
  43.  *                  to pass to LocalFree.
  44.  *
  45.  */
  46.  
  47. LPSCHMOODOC FAR PASCAL PDocumentAllocate(LPOLESERVERDOCVTBL pVtblDoc)
  48.     {
  49.     HANDLE      hMem;
  50.     LPSCHMOODOC pDoc;
  51.  
  52.     hMem=LocalAlloc(LPTR, CBSCHMOODOC);
  53.  
  54.     if (NULL==hMem)
  55.         return NULL;
  56.  
  57.     pDoc=(LPSCHMOODOC)(PSTR)hMem;
  58.  
  59.     pDoc->hMem=hMem;
  60.     pDoc->fRelease=TRUE;
  61.     pDoc->pvtbl=pVtblDoc;
  62.  
  63.     return pDoc;
  64.     }
  65.  
  66.  
  67.  
  68.  
  69. /*
  70.  * DocClose
  71.  *
  72.  * Purpose:
  73.  *  Instructs the server to unconditionally close the document.  OLESVR
  74.  *  calls DocClose when the client initiates a shutdown.  OLESVR always
  75.  *  calls this function before calling ServerRelease.
  76.  *
  77.  * Parameters:
  78.  *  pDoc            LPSCHMOODOC identifying the document affected.
  79.  *
  80.  * Return Value:
  81.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  82.  */
  83.  
  84. OLESTATUS FAR PASCAL DocClose(LPSCHMOODOC pDoc)
  85.     {
  86.     OLESTATUS       os;
  87.  
  88.     /*
  89.      * Take no action to notify user--Client will take care of that.
  90.      *
  91.      * 1.   Call OleRevokeServerDoc; resources are freed when OLESVR
  92.      *      calls DocRelease.
  93.      * 2.   Return the return value of OleRevokeServerDoc, which will
  94.      *      generally be OLE_OK.
  95.      */
  96.  
  97.     os=OleRevokeServerDoc(pDoc->lh);
  98.     return os;
  99.     }
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106. /*
  107.  * DocGetObject
  108.  *
  109.  * Purpose:
  110.  *  Requests the server application to create an OLEOBJECT structure.
  111.  *
  112.  * Parameters:
  113.  *  pDoc            LPSCHMOODOC identifying the document affected.
  114.  *  pszItem         LPSTR specifying the name of the item in a document
  115.  *                  for which the object is to be created.  If NULL, then
  116.  *                  the entire document is requested.  This could be a
  117.  *                  range of spreadsheet cells like "R1:C3-R10:C50"
  118.  *  ppObj           LPLPOLEOBJECT at which to store a pointer to the
  119.  *                  OLEOBJECT structure.
  120.  *  pClient         LPOLECLIENT that should be associated with
  121.  *                  the object in order to notify OLECLI when the object
  122.  *                  changes.
  123.  *
  124.  * Return Value:
  125.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  126.  */
  127.  
  128. OLESTATUS FAR PASCAL DocGetObject(LPSCHMOODOC pDoc, LPSTR pszItem,
  129.                                   LPLPOLEOBJECT ppObj, LPOLECLIENT pClient)
  130.     {
  131.     LPSCHMOOOBJECT    pObj;
  132.  
  133.     /*
  134.      * 1.   Allocate and initialize an OLEOBJECT structure.
  135.      * 2.   Store pClient in the object's OLEOBJECT structure for use
  136.      *      in sending notifications to the client.
  137.      * 3.   Store a pointer to the new OLEOBJECT structure in ppObj.
  138.      * 4.   Return OLE_OK if successful, OLE_ERROR_NAME if pszObj is
  139.      *      not recognized, or OLE_ERROR_MEMORY if the object could not
  140.      *      be allocated.
  141.      *
  142.      * This function is called in response to a client calling
  143.      * OleGetObject.
  144.      */
  145.  
  146.     //Allocate one from local FIXED memory.
  147.     pObj=PObjectAllocate(&pOLE->vtblObj);
  148.  
  149.     if (NULL==pObj)
  150.         return OLE_ERROR_MEMORY;
  151.  
  152.     //Remember who we created for freeing in DocRelease.
  153.     pDoc->pObj=pObj;
  154.  
  155.     //Must save this for sending notifications.
  156.     pObj->pClient=pClient;
  157.  
  158.     //Pass back the pointer to this object.
  159.     *ppObj=(LPOLEOBJECT)pObj;
  160.     return OLE_OK;
  161.     }
  162.  
  163.  
  164.  
  165. /*
  166.  * DocExecute
  167.  *
  168.  * Purpose:
  169.  *  Passes DDE Execute strings related to the document to the server
  170.  *  application.
  171.  *
  172.  * Parameters:
  173.  *  pDoc            LPSCHMOODOC identifying the document affected.
  174.  *  hCommands       HANDLE to global memory containing DDE execute strings.
  175.  *
  176.  * Return Value:
  177.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  178.  */
  179.  
  180. OLESTATUS FAR PASCAL DocExecute(LPSCHMOODOC pDoc, HANDLE hCommands)
  181.     {
  182.     /*
  183.      * 1.   Lock the hCommands handle to access the execute strings.
  184.      * 2.   Parse and execute the commands as necessary.
  185.      * 3.   DO NOT FREE hCommands.
  186.      * 4.   Return OLE_OK if successful, OLE_ERROR_COMMAND otherwise.
  187.      */
  188.  
  189.     return OLE_ERROR_COMMAND;
  190.     }
  191.  
  192.  
  193.  
  194.  
  195.  
  196. /*
  197.  * DocRelease
  198.  *
  199.  * Purpose:
  200.  *  Notifies the server when a revoked document may be destroyed.
  201.  *
  202.  * Parameters:
  203.  *  pDoc            LPSCHMOODOC identifying the document affected.
  204.  *
  205.  * Return Value:
  206.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  207.  */
  208.  
  209. OLESTATUS FAR PASCAL DocRelease(LPSCHMOODOC pDoc)
  210.     {
  211.     /*
  212.      * 1.   Free any resources allocated for this document, but
  213.      *      DO NOT free the SCHMOODOC structure itself since you may still
  214.      *      reference that structure.  This could include freeing objects,
  215.      *      however.
  216.      * 2.   Set a flag to indicate that Release has been called.
  217.      * 3.   Return OLE_OK if successful, OLE_ERROR_GENERIC otherwise.
  218.      */
  219.  
  220.     if (NULL!=pDoc->aObject)
  221.         {
  222.         DeleteAtom(pDoc->aObject);
  223.         pDoc->aObject=NULL;
  224.         }
  225.  
  226.     if (NULL!=pDoc->aClient)
  227.         {
  228.         DeleteAtom(pDoc->aClient);
  229.         pDoc->aClient=NULL;
  230.         }
  231.  
  232.  
  233.     //Release any object stored in this document.
  234.     if (NULL!=pDoc->pObj)
  235.         {
  236.         if (NULL!=pDoc->pObj->hMem)
  237.             LocalFree(pDoc->pObj->hMem);
  238.         }
  239.  
  240.     pDoc->pObj=NULL;
  241.  
  242.     pDoc->fRelease=TRUE;
  243.     return OLE_OK;
  244.     }
  245.  
  246.  
  247.  
  248.  
  249.  
  250. /*
  251.  * DocSave
  252.  *
  253.  * Purpose:
  254.  *  Instructs the server application to save the document.  If DocSave is
  255.  *  called you are assumed to have a know filename to which you can save
  256.  *  since this method is only used in linking.
  257.  *
  258.  * Parameters:
  259.  *  pDoc            LPSCHMOODOC identifying the document affected.
  260.  *
  261.  * Return Value:
  262.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  263.  */
  264.  
  265. OLESTATUS FAR PASCAL DocSave(LPSCHMOODOC pDoc)
  266.     {
  267.  
  268.     /*
  269.      * 1.   Save the document to the known filename.  How you save
  270.      *      documents is application-specific.
  271.      * 2.   Return OLE_OK if the save is successful, OLE_ERROR_GENERIC
  272.      *      otherwise.
  273.      */
  274.  
  275.     SendMessage(pGlob->hWnd, WM_COMMAND, IDM_FILESAVE, 0L);
  276.     return OLE_OK;
  277.     }
  278.  
  279.  
  280.  
  281.  
  282.  
  283. /*
  284.  * DocSetColorScheme
  285.  *
  286.  * Purpose:
  287.  *  Provides a color scheme that the client application recommends for
  288.  *  rendering graphical data.
  289.  *
  290.  * Parameters:
  291.  *  pDoc            LPSCHMOODOC identifying the document affected.
  292.  *  pPal            LPLOGPALETTE describing the recommended palette.
  293.  *
  294.  * Return Value:
  295.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  296.  */
  297.  
  298. OLESTATUS FAR PASCAL DocSetColorScheme(LPSCHMOODOC pDoc, LPLOGPALETTE pPal)
  299.     {
  300.     /*
  301.      * Servers are not required to use this palette.  The LPLOGPALETTE
  302.      * only is a convenient structure to contain the color scheme; IT DOES
  303.      * not REPRESENT A PALETTE IN STRICT TERMS!  Do NOT call CreatePalette
  304.      * and try to realize it.
  305.      *
  306.      * The color scheme contained in the LOGPALETTE structure contains
  307.      * a number of colors where the first color is the suggested foreground,
  308.      * the second the suggested background, then the first HALF of those
  309.      * remaining are suggested fill colors and the last half suggested
  310.      * line colors.  If there are an odd number of colors, give the extra
  311.      * color to the fill colors, that is, there is one less line color than
  312.      * fill colors.
  313.      */
  314.  
  315.     return OLE_ERROR_PALETTE;
  316.     }
  317.  
  318.  
  319.  
  320. /*
  321.  * DocSetDocDimensions
  322.  *
  323.  * Purpose:
  324.  *  Specifies the rectangle on the target device for which EMBEDDED
  325.  *  objects should be formatted.
  326.  *
  327.  * Parameters:
  328.  *  pDoc            LPSCHMOODOC identifying the document affected.
  329.  *  pRect           LPRECT to the device rectangle in MM_HIMETRIC units.
  330.  *
  331.  * Return Value:
  332.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  333.  */
  334.  
  335. OLESTATUS FAR PASCAL DocSetDocDimensions(LPSCHMOODOC pDoc, LPRECT pRect)
  336.     {
  337.     /*
  338.      * OLESVR will call this method when the client has resized the
  339.      * object.
  340.      *
  341.      * In this case we try to make the parent window the correct size
  342.      * to just contain the object.
  343.      */
  344.  
  345.     //First, convert the rectangle to units we can deal with MM_TEXT.
  346.     RectConvertToDevice(pGlob->hWnd, pRect);
  347.  
  348.     /*
  349.      * Tell the Polyline document to use this rectangle, also notifying
  350.      * the parent which will then resize itself.
  351.      */
  352.     SendMessage(pGlob->hWndPolyline, PLM_RECTSET, TRUE, (LONG)pRect);
  353.  
  354.     return OLE_OK;
  355.     }
  356.  
  357.  
  358.  
  359.  
  360.  
  361. /*
  362.  * DocSetHostNames
  363.  *
  364.  * Purpose:
  365.  *  Set names that should be used for window titles, only for
  366.  *  embedded objects; linked objects have their own titles as they
  367.  *  are loaded through the server application's usual file loading
  368.  *  mechanism.
  369.  *
  370.  * Parameters:
  371.  *  pDoc            LPSCHMOODOC identifying the document affected.
  372.  *  pszClient       LPSTR to the name of the client application.
  373.  *  pszObj          LPSTR to the name of the embedded object.
  374.  *
  375.  * Return Value:
  376.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  377.  */
  378.  
  379. OLESTATUS FAR PASCAL DocSetHostNames(LPSCHMOODOC pDoc, LPSTR pszClient,
  380.                                      LPSTR pszObj)
  381.     {
  382.     char        szTemp[256];
  383.  
  384.     /*
  385.      * 1.   Change the title bar to reflect the embedded state with the
  386.      *      appropriate names.
  387.      * 2.   Change the File menu to reflect the embedded state and the name
  388.      *      of the client application.
  389.      * 3.   Store the object and client names in teh OLESERVERDOC structure.
  390.      *      These will be needed later for message boxes where the name of
  391.      *      the client application must be displayed.
  392.      * 4.   Return OLE_OK if successful, OLE_ERROR_GENERIC otherwise.
  393.      */
  394.  
  395.     wsprintf(szTemp, "%s in %s", pszObj, pszClient);
  396.     WindowTitleSet(pGlob->hWnd, szTemp);
  397.     MenuEmbeddingSet(pGlob->hWnd, pszClient, TRUE);
  398.  
  399.     if (NULL!=pDoc->aObject)
  400.         DeleteAtom(pDoc->aObject);
  401.  
  402.     pDoc->aObject=AddAtom(pszObj);
  403.  
  404.     if (NULL!=pDoc->aClient)
  405.         DeleteAtom(pDoc->aClient);
  406.  
  407.     pDoc->aClient=AddAtom(pszClient);
  408.  
  409.     return OLE_OK;
  410.     }
  411.  
  412.  
  413.  
  414.  
  415.  
  416. #endif //MAKEOLESERVER
  417.